package com.hl.qzzpbackend.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hl.qzzpbackend.annotation.AuthCheck;
import com.hl.qzzpbackend.common.BaseResponse;
import com.hl.qzzpbackend.common.ResultUtils;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.companyInfo.CompanyInfoAddRequest;
import com.hl.qzzpbackend.model.dto.companyInfo.CompanyInfoUpdateRequest;
import com.hl.qzzpbackend.model.dto.companyInfo.CompanyQueryRequest;
import com.hl.qzzpbackend.model.entity.CompanyInfo;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.enums.UserRoleEnum;
import com.hl.qzzpbackend.model.vo.CompanyInfoVO;
import com.hl.qzzpbackend.service.CompanyInfoService;
import com.hl.qzzpbackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * 公司信息 接口
 *
 * @author helong
 */
@Slf4j
@Api(tags = "公司信息接口")
@RestController
@RequestMapping("/company_info")
public class CompanyInfoController {

    @Resource
    private CompanyInfoService companyInfoService;

    @Resource
    private UserService userService;


    // 添加公司信息
    @ApiOperation(value = "添加公司信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResponse<Boolean> companyInfoAdd(@Valid @RequestBody CompanyInfoAddRequest companyInfoAddRequest) {
        if(log.isInfoEnabled()){
            log.info("添加公司信息 {}", companyInfoAddRequest);
        }
        boolean result = companyInfoService.companyInfoAdd(companyInfoAddRequest);
        return ResultUtils.success(result);
    }

    // 修改公司信息
    @ApiOperation(value = "修改公司信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResponse<Boolean> companyInfoUpdate(@Valid @RequestBody CompanyInfoUpdateRequest companyInfoUpdateRequest) {
        if(log.isInfoEnabled()){
            log.info("修改公司信息 {}", companyInfoUpdateRequest);
        }
        boolean result = companyInfoService.companyInfoUpdate(companyInfoUpdateRequest);
        return ResultUtils.success(result);
    }

    // 获取公司信息
    @ApiOperation(value = "获取公司信息")
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public BaseResponse<CompanyInfoVO> getInfo(HttpServletRequest request) {
        if(log.isInfoEnabled()){
            log.info("获取公司信息");
        }
        User loginUser = userService.getLoginUser(request);
        CompanyInfoVO companyInfoVO = companyInfoService.getInfo(loginUser);
        return ResultUtils.success(companyInfoVO);
    }

    // 获取公司分页列表信息
    @AuthCheck(mustRole = {UserRoleEnum.SUPER_ADMIN, UserRoleEnum.ADMIN})
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public BaseResponse<Page<CompanyInfoVO>> getPageVO(@RequestBody CompanyQueryRequest companyQueryRequest, HttpServletRequest request) {
        if(log.isInfoEnabled()){
            log.info("获取分页查询公司信息列表 {}", companyQueryRequest);
        }
        ThrowUtils.throwIf(companyQueryRequest == null, ErrorCode.PARAMS_ERROR, "参数不能为空");

        int current = companyQueryRequest.getCurrent();
        int pageSize = companyQueryRequest.getPageSize();
        Page<CompanyInfo> companyInfoPage = companyInfoService.page(new Page<>(current, pageSize), companyInfoService.getQueryWrapper(companyQueryRequest));
        Page<CompanyInfoVO> companyInfoVOPage = new Page<>(current, pageSize, companyInfoPage.getTotal());
        List<CompanyInfoVO> companyInfoVOList = companyInfoService.getCompanyInfoVOList(companyInfoPage.getRecords());
        companyInfoVOPage.setRecords(companyInfoVOList);
        return ResultUtils.success(companyInfoVOPage);
    }


}
